package yzy.ecps.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import yzy.ecps.dao.SkuDao;
import yzy.ecps.model.BtcSku;
import yzy.ecps.model.BtcSpecValue;

@Repository
public class SkuDaoImpl extends SqlSessionDaoSupport implements SkuDao {
	String ns = "btc_sku.";
	String svns = "btc_spec_value.";

	public void saveSkusByGoodsId(List<BtcSku> skus, Long goodsId) {
		if(skus == null)
			return;
		if(skus.size()==0)
			return;
		SqlSession session = getSqlSession();
		
		for (BtcSku sku : skus) {
			//设置商品ID
			sku.setGoodsId(goodsId);
			session.insert(ns+"insert",sku);
			//保存销售单元中对应的规格值
			List<BtcSpecValue> svs = sku.getSpecValues();
			if(svs!=null && svs.size()>0){
				for (BtcSpecValue sv : svs) {
					//设置销售单元ID
					sv.setSkuId(sku.getSkuId());
					session.insert(svns+"insert", sv);
				}
			}
		}
	}

	public BtcSku getById(Long skuId) {
		return getSqlSession().selectOne(ns+"getSkuById", skuId);
	}
	
	
	public Integer getStockById(Long skuId) {
		return getSqlSession().selectOne(ns+"getStockById", skuId);
	}
	
}
